﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConnectionLayer;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Xml;

namespace BusinessLayer
{
    public static class ComunicadoActions
    {
        public static bool insereComunicado(String file, String oficina, String departamento)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "insert into Comunicado values(@file, @departamento,@oficina)";
                cmd.Parameters.AddWithValue("@file",file);
                cmd.Parameters.AddWithValue("@departamento", departamento);
                cmd.Parameters.AddWithValue("@oficina", oficina);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }

            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { Console.WriteLine(e.Message); }
            }
        }

        public static void ListaNoEcranComunicados911(ref RichTextBox rt) { 
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "select Comunicado.conteudoCom.query('/Comunicado[ DataRegisto =xs:date(\"2012-11-09\") ]')from Comunicado";
                SqlDataReader dt = cmd.ExecuteReader();
                rt.ResetText();
                while (dt.Read())
                {
                    if(dt.GetString(0) != null)
                    rt.AppendText(dt.GetString(0));
                }
               
            }
            catch (Exception e) { MessageBox.Show(e.Message); }

            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }

        }
        public static string TodosComunicados()
            {
                ConnectionFactory con = new ConnectionFactory();
           try
                {
                    con.GetConnection.Open();
                    SqlCommand cmd = con.GetConnection.CreateCommand();
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "sp_ComunicadosXML";
                    SqlParameter xml= cmd.Parameters.Add("@comunicados", System.Data.SqlDbType.Xml);
                    xml.Direction = System.Data.ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    return (String)xml.Value;
                }
           catch (SqlException e) { MessageBox.Show(e.Message); return null; }

            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }
    }
}
